package com.binhthuan.catechist.server.bus;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.binhthuan.catechist.server.domain.Venue;
import com.binhthuan.catechist.server.util.HibernateUtil;
import com.binhthuan.catechist.shared.constant.Parameter;

public class VenueBUS extends AbstractBUS {

    public List<Venue> getAllVenues() {
        return getAll(Venue.class);
    }

    public Venue getVenueById(int id) {
        return get(Venue.class, id);
    }

    public boolean deleteVenue(int id) {
        return delete(Venue.class, id);
    }

    public Venue getVenueByName(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Venue result = null;
        try{
            transaction = session.beginTransaction();
            Criteria criteria = session.createCriteria(Venue.class).add(Restrictions.eq(Parameter.NAME, name));
            result = (Venue) criteria.uniqueResult();
            
            transaction.commit();
        } catch (HibernateException he) {
            he.printStackTrace();
            transaction.rollback();
        } finally {
            session.close();
        }
        
        return result;
    }

    public boolean isInUsed(int id) {
        return true;
    }
}
